package pe.edu.upc.dsd.mercadolibre;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDAO extends BaseDAO {

	public User obtieneUser(int id) throws DAOExcepcion {
		User user = null;
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			String query = "select * from user where iduser=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			rs = stmt.executeQuery();

			if (rs.next()) {
				user = new User();
				user.setId(rs.getInt("iduser"));
				user.setNombre(rs.getString("nombre"));
			}
		} catch (Exception e) {
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return user;
	}

}
